<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchFormData" class="demo-form-inline">
      <el-form-item label="IP">
        <el-input v-model="searchFormData.ip" :clearable="true" placeholder="输入IP" style="width: 250px;" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="loadTable">查询</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary"  icon="Plus" style="margin-bottom: 10px;" @click="onAdd">新增IP</el-button>
    <el-table
      :data="pageInfo.rows"
      border
      highlight-current-row
    >
      <el-table-column
        prop="ip"
        label="IP"
        width="200"
      />
      <el-table-column
        prop="remark"
        label="备注"
        width="300"
      />
      <el-table-column
        prop="gmtCreate"
        label="添加时间"
        width="160"
      />
      <el-table-column
        prop="gmtModified"
        label="修改时间"
        width="160"
      />
      <el-table-column
        label="操作"
        width="150"
      >
        <template #default="scope">
          <el-button type="link" text  @click="onTableUpdate(scope.row)">修改</el-button>
          <el-button type="link" text  @click="onTableDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      style="margin-top: 5px"
      :current-page="searchFormData.pageIndex"
      :page-size="searchFormData.pageSize"
      :page-sizes="[5, 10, 20, 40]"
      :total="pageInfo.total"
      layout="total, sizes, prev, pager, next"
      @size-change="onSizeChange"
      @current-change="onPageIndexChange"
    />
    <!--dialog-->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      :close-on-click-modal="false"
      @close="resetForm(dialogForm)"
    >
      <el-form
        ref="dialogForm"
        :rules="dialogFormRules"
        :model="dialogFormData"
        label-width="120px"

      >
        <el-form-item prop="ip" label="IP">
          <el-input v-show="dialogFormData.id === 0" v-model="dialogFormData.ip" />
          <span v-show="dialogFormData.id > 0">{{ dialogFormData.ip }}</span>
        </el-form-item>
        <el-form-item prop="remark" label="备注">
          <el-input v-model="dialogFormData.remark" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onDialogSave">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref,reactive, onMounted } from 'vue';
import { useMessageBox } from '@/hooks/message';
import request from '@/utils/request';


const regexIP = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
const ipValidator = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入IP'))
  } else {
    if (!regexIP.test(value)) {
      callback(new Error('IP格式不正确'))
    }
    callback()
  }
}
const searchFormData= reactive({
  ip: '',
  pageIndex: 1,
  pageSize: 10
})
const pageInfo=reactive({
  rows: [],
  total: 0
})
const dialogVisible=ref(false)
const dialogTitle=ref('')
const dialogForm=ref();
const dialogFormData=reactive({
  id: 0,
  ip: '',
  remark: ''
})
const dialogFormRules=reactive({
  ip: [
    { validator: ipValidator, trigger: 'blur' },
    { min: 1, max: 64, message: '长度在 1 到 64 个字符', trigger: 'blur' }
  ],
  remark: [
    { max: 100, message: '不能超过 100 个字符', trigger: 'blur' }
  ]
})
const loadTable=async ()=>{
  const resp=await request('/api/ip/blacklist/page', searchFormData)
  Object.assign(pageInfo,resp.data)
}
const onTableUpdate=async(row)=> {
  dialogTitle.value = '修改IP'
  dialogVisible.value = true
  await nextTick()
  Object.assign(this.dialogFormData, row)
}
const onTableDelete=function(row) {
  useMessageBox().confirm(`确认要移除IP【${row.ip}】吗？`).then(async ()=>{
    const data = {
      id: row.id
    }
    await request('ip.blacklist.del', data)
    useMessage().success('删除成功')
    loadTable()
  })
}
const onDialogSave=()=> {
  dialogForm.value.validate(async(valid) => {
    if (valid) {
      const uri = dialogFormData.id ? 'ip.blacklist.update' : 'ip.blacklist.add'
      await request(uri, dialogFormData)
      dialogVisible.value = false
      loadTable()
    }
  })
}
const onSizeChange=function(size) {
    searchFormData.pageSize = size
    loadTable()
  }
const onAdd=function() {
  dialogTitle.value = '新增IP'
  dialogVisible.value = true
  dialogFormData.id = 0
}
const onPageIndexChange=function(pageIndex) {
  searchFormData.pageIndex = pageIndex
  loadTable()
}
const resetForm=(formEl)=>{
  if(!formEl){
    return;
  }
  formEl.resetFields();
}
onMounted(()=>{
  loadTable()
})
</script>
